﻿Public Class Frm_INTTradeccode

    Implements IDServer.IDServer.IDServer

    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Dim txtcodecheck As String
    Dim txtcodenamecheck As String
    Dim cmbmodulecheck As String
    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#Region "Interface Define"
    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        If AddEnable = False Then
            MessageBox.Show(My.Resources.NotAllowAdd, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If
        If Me.TabControl1.SelectedIndex = 0 Then
            Me.TabControl1.SelectedIndex = 1
        End If
        ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
        Me.txtcode.Text = ""
        Me.txtcodename.Text = ""
        SetEnable(True)
        MainForm.BtnEnable(False)
        Me.txtcode.Focus()
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Me.txtcode.Text = ""
        Me.txtcodename.Text = ""
        SetEnable(False)
        MainForm.BtnEnable(False)
        ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        GetToolEnable()
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show(My.resources.NotAllowDelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If
            If txtcode.Text = "" Then
                MessageBox.Show(Msg.Selecttradecode, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Dim tt As DataSet = ReturnDataset("select * from stor_tradecode where code='" + txtcode.Text.Trim + "'")
                If tt.Tables(0).Rows.Count < 1 Then
                    MessageBox.Show(Msg.NotExitCode, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    Dim kk As String
                    For Each ttt As DataRow In tt.Tables(0).Rows
                        kk = ttt("status")
                    Next
                    If kk = 1 Then
                        MessageBox.Show(Msg.TradeCodeIsused, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Else
                        ReturnDataset("delete  stor_tradecode where code='" + txtcode.Text.Trim + "'")
                        MessageBox.Show(My.resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                        viewdata()
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, "error", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        If EditEnable = False Then
            MessageBox.Show(My.resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        isnull()
        viewdata()
        SetEnable(False)
        MainForm.BtnEnable(False)
        GetToolEnable()
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect
        QueryData()
    End Function
#End Region

#Region "Sub Define"
    Private Sub Frm_INTTradeccode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'addcodegroup()
        addmodule()
        txtcode.Focus()
        viewdata()
        SetEnable(False)
        Me.btnquery.Enabled = FindEnable
    End Sub
    Private Sub SetEnable(ByVal b As Boolean)
        Try
            Me.txtcode.Enabled = b
            Me.txtcodename.Enabled = b
            Me.cmbmodule.Enabled = b
            Me.cmbcodegroup.Enabled = b
            Me.btnsave.Enabled = b
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub addmodule()
        Try
            With cmbmodule
                .Items.Clear()
                .Items.Add("+" + "庫存")
                .Items.Add("-" + "庫存")
            End With
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, "error", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try

    End Sub
    Private Sub isnull()
        Try
            txtcodecheck = txtcode.Text
            txtcodenamecheck = txtcodename.Text
            cmbmodulecheck = cmbmodule.Text
            If txtcodecheck.Length = 0 Then
                MessageBox.Show(My.Resources.notnullcode, My.Resources.Errshow, MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf txtcodenamecheck.Length = 0 Then
                MessageBox.Show(Msg.notnullcodename, My.Resources.Errshow, MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf cmbmodulecheck.Length = 0 Then
                MessageBox.Show(Msg.Notnullmodule, My.Resources.Errshow, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                isexist()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try

    End Sub
    Private Sub viewdata()
        Try
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter
            Dim sqlstr As String = "select code as 代碼, name  as 交易名稱, addornot as 操作庫存,status as 狀態 from stor_tradecode "
            ds = ReturnDataset(sqlstr)
            DataGridView1.DataSource = ds.Tables(0).DefaultView
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub isexist()
        Try
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter
            Dim sqlstr As String = "select * from stor_tradecode where code='" + txtcode.Text.Trim + "'"
            ds = ReturnDataset(sqlstr)
            If ds.Tables(0).Rows.Count > 0 Then
                MessageBox.Show(Msg.IsExitedcode, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                savedata()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub savedata()
        Try
            Dim sqlstr As String = "insert into stor_tradecode (code,name,addornot,status) values('" & txtcode.Text & "','" & txtcodename.Text & "','" & cmbmodule.Text.Substring(0, 1) & "','0')"
            ReturnDataset(sqlstr)
            MessageBox.Show(My.Resources.AddSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub cmbmodule_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Private Sub txtcode_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub
    Private Sub txtcode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If txtcode.Text.Length > 2 Then
                MessageBox.Show(Msg.NotvalidataCode, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtcode.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub btnquery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub
    Private Sub btnsave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        MainForm.ToolStripSave_Click(Nothing, Nothing)
    End Sub
    Private Sub btndelete_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        Try
            MainForm.ToolStripDelete_Click(Nothing, Nothing)
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnquery.Click

        Try
            MainForm.ToolStripFind_Click(Nothing, Nothing)

        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub QueryData()
        Try
            Dim strsql As String = ""
            strsql = "select code as 代碼, name  as 交易名稱, addornot as 操作庫存,status as 狀態,codegroup 組別  from stor_tradecode  where 1=1 "

            If txtquery1.Text.Length > 0 Then
                strsql += " and code ='" + txtquery1.Text + "' "
            End If

            If txtquery2.Text.Length > 0 Then
                strsql += " and name like '" + txtquery2.Text + "%' "
            End If

            Dim tt As DataSet = ReturnDataset(strsql)
            If tt.Tables(0).Rows.Count > 0 Then
                DataGridView2.DataSource = tt.Tables(0)
            Else
                MessageBox.Show(Msg.NotExitCode, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub GetToolEnable()
        Try
            With MainForm
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable
                .ToolStripDelete.Enabled = DeleteEnable
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With
        Catch ex As Exception
            MessageBox.Show(ex.ToString + Err.Description, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

End Class
